getclient();
function getclient() {
    Ajax({
        url: "http://localhost/h5/php/kehu.php",
        success(res) {
          
            const tbody = document.querySelector("tbody")
          
            sessionStorage.setItem("allkehuInfo", res)
            res = JSON.parse(res)
            sessionStorage.setItem("pageCurrent", 1);
            const pageCount = Math.ceil(res.length / 8)
            sessionStorage.setItem("pageCount", pageCount)
           
            let result = res.slice(0, 8)
            renderX(result)

        }
    })
}

function renderX(result) {
    const tbody = document.querySelector("tbody");
    tbody.innerHTML = "";
    result.forEach(item => {
        let str = `
          
        <tr>
        <td>${item.h_Id}</td>
        <td>${item.h_number}</td>
        <td>${item.h_overname}</td>
        <td>${item.h_iphone}</td>
        <td>${item.h_overfrom}</td>
        
        <td>
        <button type="button" class="btn btn-warning btn-sm" data-bs-toggle="modal" data-o='update' data-bs-target="#kehuAdd" data-id='${item.h_Id}'>修改</button>
        <button type="button" class="btn btn-danger btn-sm" data-o='del' data-id='${item.h_Id}'>删除</button>
        </td>
    </tr>
        `
        tbody.innerHTML += str;
    })
}


deleCtcliEnt();
function deleCtcliEnt() {
    const tbody = document.querySelector("tbody");
    tbody.addEventListener("click", function (e) {
        e = e || window.event
        if (e.target.dataset.o == 'del') {
            const flag = window.confirm("你确定要删除嘛")
            if (flag) {
                Ajax({
                    url: "http://localhost/h5/php/deletkehu.php",
                    data: { id: e.target.dataset.id },
                    success(res) {
                        location.reload();
                    }
                })
            }
        }
        if (e.target.dataset.o == 'update') {
            const flag = window.confirm("你确定要修改嘛?")
            if (flag) {
                Ajax({
                    url: "http://localhost/h5/php/kehuquery.php",

                    data: { id: e.target.dataset.id },
                    success(res) {
                        // console.log(res);
                        res = JSON.parse(res)
                        let number = document.getElementById("updateaddaMount");
                        let overname = document.getElementById("updateaddkehuName");
                        let iphone = document.getElementById("updateaddKehuIphone");
                        let overfrom = document.getElementById("updateaddSite");
                        let kehuId = document.getElementById("updateaddkehuId");


                        number.value = res.h_number;
                        overname.value = res.h_overname;
                        iphone.value = res.h_iphone;
                        overfrom.value = res.h_overfrom;
                        kehuId.value = res.h_Id;


                    }
                })
            }
        }
    })
}

function kehuupdate() {

    let number = document.getElementById("updateaddaMount").value
    let overname = document.getElementById("updateaddkehuName").value
    let iphone = document.getElementById("updateaddKehuIphone").value
    let overfrom = document.getElementById("updateaddSite").value
    let kehuId = document.getElementById("updateaddkehuId").value

    Ajax({
        url: "http://localhost/h5/php/kehuUpdate.php",
        type: "POST",
        data: { number, overname, iphone, overfrom, kehuId },
        success(res) {
            sessionStorage.setItem("allkehuInfo", res)
            location.reload();
        }
    })
}



function addkehuInfo() {

    let number = document.getElementById("addaMount").value
    let overname = document.getElementById("addkehuName").value
    let iphone = document.getElementById("addKehuIphone").value
    let overfrom = document.getElementById("addSite").value
    let kehuId = document.getElementById("addkehuId").value

    if (!number || !overname || !iphone || !overfrom || !kehuId)
        return window.alert("请输入完整的数据")

    Ajax({
        url: "http://localhost/h5/php/kehuadd.php",
        type: "POST",
        data: { number, overname, iphone, overfrom, kehuId, },
        success(res) {
            location.reload();
        }
    })
}


function firstPage() {

    sessionStorage.setItem("pageCurrent", 1)
    let result = JSON.parse(sessionStorage.getItem("allkehuInfo"))
    result = result.slice(0, 8)
    renderX(result)

}
function upPage() {

    let pageCurrent = +sessionStorage.getItem("pageCurrent") - 1

    if (pageCurrent <= 1) {
        pageCurrent = 1;
    }
    sessionStorage.setItem("pageCurrent", pageCurrent)
    let result = JSON.parse(sessionStorage.getItem("allkehuInfo"))
    result = result.slice((pageCurrent - 1) * 8, pageCurrent * 8);
    renderX(result)
}


function downPage() {
    

    let pageCurrent = +sessionStorage.getItem("pageCurrent") + 1
    let result = JSON.parse(sessionStorage.getItem("allkehuInfo"))
    let pageCount = +sessionStorage.getItem("pageCount")
    if (pageCurrent > pageCount) {
        pageCurrent = pageCount;
    }
    sessionStorage.setItem("pageCurrent", pageCurrent)
    
    result = result.slice((pageCurrent - 1) * 8, pageCurrent * 8)
    renderX(result)

}

function lastPage() {
   

    let pageCount = +sessionStorage.getItem("pageCount")
    sessionStorage.setItem("pageCurrent", pageCount)
    let result = JSON.parse(sessionStorage.getItem("allkehuInfo"))
    result = result.slice((pageCount - 1) * 8)
    renderX(result)

}

function selectLike() {
    

    let like = document.querySelector("#like")
  
    if (like.value.trim().length <= 0) return alert("大哥正常点呐")
    Ajax({
        type: "GET",
        url: "http://localhost/h5/php/kehulike.php",
        data: { name: like.value },
        success(res) {
            sessionStorage.setItem("allkehuInfo", res)
            sessionStorage.setItem("pageCount", 1)
            let result = JSON.parse(res)
            sessionStorage.setItem("pageCurrent", Math.ceil(result.length / 10))
            result = result.slice(0, 10)
            renderX(result)
        }
    })
}







